﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Deeper.Common
{
    public class FileHelper
    {
        //private const Encoding defaultEncoding = new UTF8Encoding();

        /// <summary>
        /// 拷贝文件
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目标文件名</param>
        /// <param name="overwrite">当目标文件存在时是否覆盖</param>
        /// <returns>操作是否成功</returns>
        public static bool CopyFile(string sourceFileName, string destFileName, bool overwrite = true)
        {
            if (!System.IO.File.Exists(sourceFileName))
            {
                throw new FileNotFoundException(sourceFileName + "文件不存在！");
            }
            if (!overwrite && System.IO.File.Exists(destFileName))
            {
                return false;
            }
            try
            {
                System.IO.File.Copy(sourceFileName, destFileName, true);
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 返回文件是否存在
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>是否存在</returns>
        public static bool FileExists(string filename)
        {
            return System.IO.File.Exists(filename);
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="FilePath">文件的物理地址</param>
        /// <returns></returns>
        public static bool DeleteFile(string FilePath)
        {
            try
            {
                System.IO.File.Delete(FilePath);
                return true;
            }
            catch
            {
                //errorMsg = "删除不成功！";
                return false;
            }
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="DirPath">文件夹名称</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteDir(string DirPath)
        {
            try
            {
                if (Directory.Exists(DirPath))
                    Directory.Delete(DirPath, true);
                return true;
            }
            catch
            {
                return false;
            }
        }




        #region 读写文件


        /// <summary>
        /// ReadTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="encoding">编码</param>
        /// <returns>文本内容</returns>
        public static string ReadTextFile(string fileName, Encoding encoding)
        {
            string text;

            using (StreamReader sr = new StreamReader(fileName, encoding))
            {
                text = sr.ReadToEnd();
            }

            return text;
        }

        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        /// <param name="append">是否添加到文本后面</param>
        /// <param name="createDir">是否创建目录</param>
        /// <param name="encoding">编码</param>
        public static void WriteTextFile(string fileName, string text, Encoding encoding, bool append = true, bool createDir = true)
        {

            if (createDir)
            {
                string dirName = Path.GetDirectoryName(fileName);

                if (!Directory.Exists(dirName))
                {
                    Directory.CreateDirectory(dirName);
                }
            }

            using (StreamWriter sw = new StreamWriter(fileName, append, encoding))
            {
                sw.Write(text);
            }
        }

        #endregion
    }
}
